class Thread6 extends Thread {
    static int ticket = 20;
    private String name;
    private Object obj;

    public Thread6(String name, Object obj) {
        this.name = name;
        this.obj = obj;
    }

    @Override
    public void run() {
        while (ticket > 0) {
            synchronized (obj) {
                if (ticket > 0) {
                    System.out.println(name + ":售卖后剩余" + --ticket + "张票");
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

public class T6 {
    public static void main(String[] args) throws InterruptedException {
        Object o = new Object();
        Thread t1 = new Thread6("窗口一", o);
        Thread t2 = new Thread6("窗口二", o);
        Thread t3 = new Thread6("窗口三", o);
        t1.start();
        t2.start();
        t3.start();

    }
}
